#ifndef __SELECTED_POLYGON_H__
#define __SELECTED_POLYGON_H__

#pragma once

#include "Polygon.h"
#include "MultiPolygon.h"


// class support the multiple-selected operations
class CSelectedPolygon
{
private:
	CMultiPolygon		*m_pMultiPolygon;	// the pointer to polygons array
	std::vector<int>	m_arSelectedIdx;	// the selected polygons
	int					m_nCurSelectIdx;	// index of the current operation polygon
	BOOL				m_bGrouped;			// if these selected polygons are grouped or not
	std::vector<int>	m_nOringVNs;			//origin vertex number;	
	CVertex				m_Handles[POLYHNDL_NUMBER];	// the handles of the group

	double	XMin() { return m_Handles[ POLYHNDL_UPPERLEFT  ].x; }
	double	YMin() { return m_Handles[ POLYHNDL_UPPERLEFT  ].y; }
	double	XMax() { return m_Handles[ POLYHNDL_LOWERRIGHT ].x; }
	double	YMax() { return m_Handles[ POLYHNDL_LOWERRIGHT ].y; }
	//int		MaxIndex();

public:
	CSelectedPolygon(void);
	~CSelectedPolygon(void);

	void	Clear();
	void	SetMultiPolygon( CMultiPolygon *pMP );
	int		FindFirstPolyContainPoint( double px, double py, int flag );
	
	void	UpdateChanges();
	void	ComputeBoundingBox();
	void	ComputeHandles();
	void	ComputeAnchor();
	void	Resample( int nNumVertices, BOOL bKeepOrgVertices );

	void	SetGrouped( BOOL group );
	BOOL	GetGrouped() { return m_bGrouped; }
	HandleMode	PointHandleMode( double px, double py );

	// drawings
	void		Draw( CDC *pDC, bool bDrawVertex );
	void		DrawBoundingBox( CDC *pDC );
	void		DrawHandles( CDC *pDC );

	// transformations
	void		Translate( double px, double py );
	void		TranslateAnchor( double px, double py );
	double		GetScaleXScale( double x0, double x1 );
	void		ScaleMoveX( double x0, double x1 );
	double		GetScaleYScale( double y0, double y1 );
	void		ScaleMoveY( double y0, double y1 );
	void		ScaleMove( double x0, double y0, double x1, double y1 );
	void		ScaleMoveUnified( double x0, double y0, double x1, double y1 );
	void		Scale( double scaleX, double scaleY );
	double		GetShearXScale( double x0, double y0, double x1, double y1 );
	void		ShearMoveX( double x0, double y0, double x1, double y1 );
	double		GetShearYScale( double x0, double y0, double x1, double y1 );
	void		ShearMoveY( double x0, double y0, double x1, double y1 );
	double		GetRotateAngle( double x0, double y0, double x1, double y1 );
	void		RotateMove( double x0, double y0, double x1, double y1 );
	void		Rotate( double angle );

	void		FlipHorizontal();
	void		FlipVertical();
	void		MakeClosed(bool close);
	void		SwitchClosed(bool close);
	void		Connect();
	void		ReverseVertexOrder();
	void		SetAnchorAsFirstVertex();
	void		SetAnchorAsHandle(HandleMode mode);
	void		SetAnchor( double px, double py );
	void		SetAnchorAsCenter();

	CVertex		GetHandle( HandleMode mode );
	CVertex		GetGroupHandle( HandleMode mode );
	CVertex		GetAnchor();
	int			Size();
	void		SetByRubberRegion( CPoint pt1, CPoint pt2 );
	void		SelectAll();
	void		RemoveSelectedPolygons();
	void		Clone();
	void		ZoomHandles( double ratio );

	void		SetFilledColor( COLORREF color );
	void		SetFilled( bool filled );

	void		BringToFront();
	void		SendToBack();
	void		BringForward();
	void		SendBackward();

	int			IsSelected( int poly );
	CPolygon	*GetPolygon( int idx );
	void		SubAllEdge(int nSeg);
	int			GetAt( int idx );

	void		AlignLefts();
	void		AlignRights();
	void		AlignTops();
	void		AlignBottoms();
	void		AlignCenters();
	void		AlignMiddles();
	void		AlignAnchors();
	void		AlignDistributeHorizontally();
	void		AlignDistributeVertically();
	void		AlignOnTopObject(BOOL bAlignTangent);

	void		Add( int poly );
	void		RemoveAt( int idx );

	void		DelaunayTriangulation();
	int			ResampleByRatio(float rt);
	void		SaveOriginSize();
};

#endif	// __SELECTED_POLYGON_H__
